package applet.attendee.unlocked.components.events;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import data.CMSRes;
import data.event.Event;

public class EventDialog extends JDialog
{
	private static final long serialVersionUID = 3788587118056408815L;

	private JLabel name;
	private JLabel location;
	private JLabel startTime;
	private JLabel endTime;
	private JList attendeeList;
	private JTextArea description;
	
	public EventDialog(Event event)
	{
		super(CMSRes.frame(), "Event Overview", true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		defineComponents(event);
		JPanel panel = new JPanel(new BorderLayout());
			panel.add(defineSummary(), BorderLayout.NORTH);
			panel.add(defineDescription(), BorderLayout.CENTER);
			panel.add(defineList(), BorderLayout.SOUTH);
		add(panel, BorderLayout.CENTER);
		
		pack();
		if(getHeight() > 400)
			setSize(getWidth() + 30, 400);
		setResizable(false);
	}
	
	private void defineComponents(Event event)
	{
		name = new JLabel(event.name() + ": ");
		name.setFont(name.getFont().deriveFont(Font.BOLD));
		name.setFont(name.getFont().deriveFont(16f));
		
		location = new JLabel(event.location());
		location.setFont(location.getFont().deriveFont(Font.BOLD));
		
		startTime = new JLabel(event.startTimeText());
		startTime.setFont(startTime.getFont().deriveFont(Font.BOLD));
		
		endTime = new JLabel(event.endTimeText());
		endTime.setFont(endTime.getFont().deriveFont(Font.BOLD));
		
		String[] attendees = event.attendees().toArray(new String[0]);
		if(attendees.length == 0)
			attendees = new String[] { "(none attending)" };
		attendeeList = new JList(attendees);
		attendeeList.setOpaque(false);
		
		description = new JTextArea(event.description());
		description.setEditable(false);
		description.setLineWrap(true);
		description.setWrapStyleWord(true);
	}
	
	private JPanel defineSummary()
	{
		JPanel panel2 = new JPanel(new BorderLayout());
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
			layoutHelper.add(name, BorderLayout.WEST);
			
		panel2.add(layoutHelper, BorderLayout.NORTH);
		
		JPanel panel = new JPanel(new GridLayout(0, 2));
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Location:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
			
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(location, BorderLayout.WEST);
			
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Start time:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
			
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10
					));
			layoutHelper.add(startTime, BorderLayout.WEST);
			
		panel.add(layoutHelper);
			
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("End time:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
			
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(endTime, BorderLayout.WEST);
			
		panel.add(layoutHelper);
		
		panel2.add(panel, BorderLayout.CENTER);
		
		return panel2;
	}

	private JPanel defineDescription()
	{
		JPanel panel = new JPanel(new BorderLayout());
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel labelHelper = new JLabel("Event Description:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		
		panel.add(layoutHelper, BorderLayout.NORTH);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(0, 60, 10, 10));
			layoutHelper.setPreferredSize(new Dimension(250, 200));
				layoutHelper.add(new JScrollPane(description), BorderLayout.CENTER);

		panel.add(layoutHelper, BorderLayout.CENTER);
		
		return panel;
	}
	
	private JPanel defineList()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(0, 60, 10, 10));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Attending:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper, BorderLayout.NORTH);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.add(attendeeList, BorderLayout.WEST);
		panel.add(layoutHelper, BorderLayout.CENTER);
		return panel;
	}
}